using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    public class ServiceProviderTypeMessageTypeManager
    {
        /// <summary>
        /// Searches for a link between MessageTypes and ServiceProviderTypes either by MessageType or ServiceProviderType
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="messageTypeID"></param>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns></returns>
        public static ServiceProviderTypeMessageTypes ServiceProviderTypeMessageTypeLinkSearch(HSDPrincipal currentPrincipal,
            string messageTypeID, string serviceProviderTypeID)
        {
            string content = string.Format("<{0}>{1}</{0}><{2}>{3}</{2}>", "MessageTypeID", messageTypeID, "ServiceProviderTypeID", serviceProviderTypeID);
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeMessageTypeSearch", content);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1059, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (ServiceProviderTypeMessageTypes)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(ServiceProviderTypeMessageTypes));
        }

        public static bool ServiceProviderTypeMessageTypeLinkInsert(HSDPrincipal currentPrincipal, int messageTypeID,
            int sourceServiceProviderTypeID, int destinationServiceProviderTypeID)
        {
            ServiceProviderTypeMessageTypeLink sptMessageTypeLink = new ServiceProviderTypeMessageTypeLink();
            sptMessageTypeLink.MessageTypeID = messageTypeID;
            sptMessageTypeLink.SourceServiceProviderTypeID = sourceServiceProviderTypeID;
            sptMessageTypeLink.DestinationServiceProviderTypeID = destinationServiceProviderTypeID;

            return ServiceProviderTypeMessageTypeLinkInsert(currentPrincipal, sptMessageTypeLink);
        }

        public static bool ServiceProviderTypeMessageTypeLinkInsert(HSDPrincipal currentPrincipal, ServiceProviderTypeMessageTypeLink sptMessageTypeLink)
        {
            string sptMessageTypeLinkXml = AdapterHelper.ConstructObjectXml(sptMessageTypeLink, typeof(ServiceProviderTypeMessageTypeLink));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, sptMessageTypeLinkXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1054, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        public static bool ServiceProviderTypeMessageTypeLinksInsert(HSDPrincipal currentPrincipal, ServiceProviderTypeMessageTypeLinks sptMessageTypeLinks)
        {
            string sptMessageTypeLinksXml = AdapterHelper.ConstructObjectXml(sptMessageTypeLinks, typeof(ServiceProviderTypeMessageTypeLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, sptMessageTypeLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1058, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }


        public static bool ServiceProviderTypeMessageTypeLinkDelete(HSDPrincipal currentPrincipal, int serviceProviderTypeMessageTypeID)
        {
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeMessageTypeID", serviceProviderTypeMessageTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1055, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        public static bool ServiceProviderTypeMessageTypeLinksDelete(HSDPrincipal currentPrincipal, ServiceProviderTypeMessageTypeLinks sptMessageTypeLinks)
        {
            string sptMessageTypeLinksXml = AdapterHelper.ConstructObjectXml(sptMessageTypeLinks, typeof(ServiceProviderTypeMessageTypeLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, sptMessageTypeLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1069, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

    }
}
